package com.bto.solar.work.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bto.solar.work.common.R;
import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.dao.IDesignService;
import com.bto.solar.work.entity.Design;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.utils.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 设计 前端控制器
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@RestController
@RequestMapping("/design")
public class DesignController {
    @Autowired
    private IDesignService designService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;

    @ApiModelProperty("查询设计变更记录")
    @GetMapping("record")
    public R getDesignUpdateRecord(@RequestParam String orderId, @RequestParam(required = false) String designId) {
        List<Design> list = designService.lambdaQuery()
                .eq(Design::getOrderId, orderId)
                .eq(StringUtils.isNotEmpty(designId), Design::getId, designId)
                .orderByDesc(Design::getCreateTime).list();
        return R.ok(list);
    }

    @ApiModelProperty("查询设计变更统计")
    @GetMapping("record-count")
    public R getDesignUpdateRecordCount() {
        BizStageEnum includeStageId = BizStageEnum.DESIGN;
        BizTaskEnum includeTaskId = BizTaskEnum.TASK_CSPSXX;
        List<BizTaskStateEnum> includeStates = Arrays.asList(BizTaskStateEnum.APPROVAL_PASS_BTO);
        BizTaskEnum excludeTaskId = BizTaskEnum.TASK_CSPSXX_UPDATE;
        Set<String> designPassIds = btoOrderStateService.getOrderIdsByStates(null, excludeTaskId, null,
                includeStageId, includeTaskId, includeStates, null, null);

        JSONObject designCountJsonObj = new JSONObject();
        designCountJsonObj.put("taskId", includeTaskId.getTaskId());
        designCountJsonObj.put("taskName", BizTaskEnum.TASK_CSPSXX.getTaskName());
        designCountJsonObj.put("stateId", BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId());
        designCountJsonObj.put("stateName", "一键变更");
        designCountJsonObj.put("count", designPassIds.size());

        includeStageId = BizStageEnum.DESIGN;
        includeTaskId = BizTaskEnum.TASK_CSPSXX_UPDATE;
        BizStageEnum excludeStage = BizStageEnum.CONSTRUCT;
        List<BizTaskStateEnum> excludeStates = Arrays.asList(BizTaskStateEnum.APPROVAL_PASS_BTO);

        Set<String> designUpdateIds = btoOrderStateService.getOrderIdsByStates(excludeStage, null, excludeStates,
                includeStageId, includeTaskId, null, null, null);


        JSONObject taskCountJsonObj = new JSONObject();
        taskCountJsonObj.put("taskId", includeTaskId.getTaskId());
        taskCountJsonObj.put("taskName", BizTaskEnum.TASK_CSPSXX_UPDATE.getTaskName());
        taskCountJsonObj.put("stateId", null);
        taskCountJsonObj.put("stateName", "设计变更");
        taskCountJsonObj.put("count", designUpdateIds.size());

        JSONArray res = new JSONArray();
        res.add(designCountJsonObj);
        res.add(taskCountJsonObj);
        return R.ok(res);
    }

}
